package edu.whut.mall.admin.controller;

import edu.whut.mall.admin.domain.ResourceParams;
import edu.whut.mall.admin.service.ISysResourcesService;
import edu.whut.mall.admin.vo.ResourceVo;
import edu.whut.mall.common.api.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @program: d9lab-mall
 * @description: 资源控制器
 * @author: Wayne
 * @create: 2020-04-14 12:31
 **/
@Slf4j
@Api(value="资源管理器",tags = {"资源管理器"})
@RestController
@RequiredArgsConstructor
@RequestMapping("/resource")
public class RestResourceController {


    private final ISysResourcesService sysResourcesService;

    @ApiOperation("添加资源")
    @PostMapping(value="/addOneResource")
    public CommonResult addOneResource(@Valid @RequestBody ResourceParams resourceParams){
       return sysResourcesService.insertOneResource(resourceParams);
    }


    @ApiOperation("更新资源")
    @PostMapping("/updateResource")
    public CommonResult updateResource(@Valid @RequestBody ResourceParams resourceParams){
        int result = sysResourcesService.updateResource(resourceParams);
        if (result >= 0) {
            return CommonResult.success("更新资源成功");
        }
        return CommonResult.failed("更新添加失败");
    }

    @ApiOperation("获取所有资源")
    @GetMapping("/getResourceList")
    public CommonResult getResourceList(){
        List<ResourceVo> resourceVoList=sysResourcesService.listResourceVos();
        if (!resourceVoList.isEmpty()) {
            return CommonResult.success(resourceVoList, "获取所有资源成功");
        }
        return CommonResult.failed("获取所有资源失败");
    }

    @ApiOperation("删除资源")
    @GetMapping("/deleteOneResource")
    public CommonResult deleteOneResource(@NotNull @RequestParam Long resourceId){
        int result = sysResourcesService.deleteOneResource(resourceId);
        if (result >= 0) {
            return CommonResult.success("删除资源成功");
        }
        return CommonResult.failed("删除添加失败");
    }

}
